package com.data;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.util.Log;

import com.bean.JobBean;
import com.constant.Constant;
import com.net.ClientError;
import com.net.ClientException;
import com.net.RequestParameter;
/**
 * 就业信息数据
 * 该接口中不需要传递参数
 * @author mjh
 *
 */
public final class JobData extends BaseData
{
	private final static String TAG = "job_data";

	private ArrayList<JobBean> mJobList;
	
	
	
	@Override
	public void startParse(RequestParameter parameter) throws Exception, ClientException
	{
		// 此处的参数为null，不需要解析
		String stream = openConnection();
		if(stream == null)
			return;
		
		readStream(stream);
		
	}
	
	private String openConnection() throws IOException, ClientException
	{
		
		URL url = new URL("http://xhjy.xhu.edu.cn");
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestProperty("Referer", "http://www.xhu.edu.cn/");
		connection.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*");
		connection.setRequestProperty("Host", "");
		connection.setRequestMethod("GET");
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(false);
		connection.setConnectTimeout(Constant.SET_CONNECTION_TIMEOUT);
		connection.setReadTimeout(Constant.SET_READ_TIMEOUT);
		
		if (connection.getResponseCode() != 200)
		{
			Log.i(TAG, "就业信息数据获取失败");
			throw new ClientException("网络连接失败", ClientError.ERROR_CONNECTION);
		}
		
//		// 获取网页源代码
//		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "gb2312"));
//		StringBuffer s_html = new StringBuffer();
//		String line = null;
//		while( (line = reader.readLine()) != null)
//		{
//			s_html.append(line + "\n");
//		}
//		reader.close();
//		
//		//System.out.println(s_html.toString());
//		
//		return s_html.toString();
		return getWebSource(connection.getInputStream(), "gb2312");
		
	}
	
	/**
	 * 解析近期招聘信息
	 * 格式如下：
	 * <table>
	 * 		<tr>标题数据</tr>
	 * 
	 *      <tr  height="20"  bgcolor="#FFFFFF">
	 *           <td align="left">
	 *           	<a href="news_zc.asp?id=7021" style="font-size:12px;color:red;font-weight:normal " target="_blank">　
	 *           	重庆大学城市科技学院（复试）</td>
	 *           <td align="center" style="color:red">2012-10-20</td>
	 *           <td align="center" style="color:red">08:00</td>
	 *           <td align="center" style="color:red">地点改在交大</td> 
	 *           </a></li>	
	 *      </tr>
	 *      
	 *      <tr  height="20"  bgcolor="#DDEEFF">
	 *      	<td align="left">
	 *      		<a href="news_zc.asp?id=7184" style="font-size:12px;color:red;font-weight:normal " target="_blank">　
	 *      		西华大学2012年公务员考试公益公开课公告</td>
	 *      		<td align="center" style="color:red">2012-10-20</td>
	 *      		<td align="center" style="color:red">09:00</td>
	 *      		<td align="center" style="color:red">3C401</td>
	 *      		</a></li>
	 *      </tr>
	 *      .....
	 *  </table>
	 *  注意上面html不规范
	 * 
	 * @param stream:html数据流
	 * 
	 * @throws IOException
	 */
	public void readStream(String stream) throws IOException
	{
		Document document = Jsoup.parse(stream);
		
		Elements table = document.getElementsByAttributeValue("valign", "top");
		Element td6 = table.get(7);
		
		table = td6.select("tr");
		
		Log.i(TAG, "开始解析招聘信息");
		
		mJobList = new ArrayList<JobBean>();
		
		// 跳过标题数据解析,此处的html标签不规范
		for(int i = 1, length = table.size(); i < length; i ++)
		{
			JobBean bean = new JobBean();
			
			Element tr = table.get(i);
			Element td = tr.child(0);
			bean.title = td.child(0).ownText();
			bean.url = "http://xhjy.xhu.edu.cn/" + td.child(0).attr("href");
			bean.referer = "http://xhjy.xhu.edu.cn/";
			bean.date = tr.child(1).ownText();
			bean.time = tr.child(2).ownText();
			bean.site = tr.child(3).ownText();
			
			mJobList.add(bean);
			
		}
		
		Log.i(TAG, "数据解析成功");
		
	} // readStream
	
	public ArrayList<JobBean> getJobList()
	{
		return this.mJobList;
	}
	
	public void printData()
	{
		for(int i = 0; i < mJobList.size(); i++)
		{
			JobBean bean = mJobList.get(i);
			System.out.println(bean.title);
			System.out.println(bean.url);
			System.out.println(bean.referer);
			System.out.println(bean.date);
			System.out.println(bean.time);
			System.out.println(bean.site);
		}
	}
}

